import { createParameterDecorator } from "maishu-node-mvc";
import { ServerContext } from "maishu-chitu-admin";
import { ServerContentData } from "./website-config";
import { EntityManager, Repository } from "typeorm";
import path = require("path");
import { User } from "./entities";
import { DataContext } from "common";
import { ConnectionConfig } from "mysql";

export class MemberDataContext extends DataContext {

    user: Repository<User>;

    constructor(manager: EntityManager) {
        super(manager);
        this.user = manager.getRepository(User);
    }
}

export async function createDataContext(db: ConnectionConfig) {
    let dc = await DataContext.createInstance(db, MemberDataContext, entitiesPath);
    return dc
}

let entitiesPath = path.join(__dirname, "entities.js");
export let memberDataContext = createParameterDecorator<MemberDataContext>(
    async (a, b, serverContext: ServerContext<ServerContentData>) => {
        let dc = await DataContext.createInstance(serverContext.data.db, MemberDataContext, entitiesPath);
        return dc
    }
)
